// set function parseTime,formatTime to filter
export { parseTime, formatTime } from '@/utils';

function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return time + label + 's';
}

export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);
  if (between < 3600) {
    return pluralize(~~(between / 60), ' minute');
  } else if (between < 86400) {
    return pluralize(~~(between / 3600), ' hour');
  } else {
    return pluralize(~~(between / 86400), ' day');
  }
}

/* 数字 格式化*/
export function numberFormatter(num, digits) {
  const si = [
    { value: 1E18, symbol: 'E' },
    { value: 1E15, symbol: 'P' },
    { value: 1E12, symbol: 'T' },
    { value: 1E9, symbol: 'G' },
    { value: 1E6, symbol: 'M' },
    { value: 1E3, symbol: 'k' }
  ];
  for (let i = 0; i < si.length; i++) {
    if (num >= si[i].value) {
      return (num / si[i].value + 0.1).toFixed(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + si[i].symbol;
    }
  }
  return num.toString();
}

export function toThousandFilter(num) {
  return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','));
}
/**
 * 映射字典---对象
 */
export function filterDicts(value, arg1) {
  if (arg1) {
    let val = '';
    value = Number(value) ? Number(value) : value;
    Object.keys(arg1).forEach(key => {
      if (String(value) === String(key)) {
        val = arg1[key];
      }
    });
    console.log(val);
    return val;
  }
}
/**
 * 映射字典2----数组
 */
export function filterDict(value, arg1, arg2) {
  if (arg1) {
    let val = '';
    for (let i = 0; i < arg1.length; i++) {
      const obj = arg1[i];
      if (String(value) === String(obj.code)) {
        val = obj.name;
      }
    }
    return val || '-';
  }
}
/**
 * 映射字典2----数组
 */
export function filterDictForId(value, arg1, arg2) {
  if (arg1) {
    let val = '';
    for (let i = 0; i < arg1.length; i++) {
      const obj = arg1[i];
      if (String(value) === String(obj.id)) {
        val = obj.name;
      }
    }
    return val || '-';
  }
}
/** 元转万元 */
export function yuanToTenThousand(yuan) {
  return (yuan / 10000).toFixed(2);
}
